<template>
  <div class="app-container">
    <!-- 顶部查询条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="批次编码" prop="batchId">
        <el-input
          v-model="queryParams.batchId"
          placeholder="请输入批次编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部操作栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              type="primary"
            >导入</el-button>
      </el-col>
    </el-row>

    <!-- 内部列表 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" border>
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column label="批次编码" align="center" prop="batchId" width="200"/>
      <el-table-column label="状态" align="center" width="100" prop="cstatus">
        <template slot-scope="scope">
          <el-tag size="medium" :type="statusType(scope.row.cstatus)">{{ statusText(scope.row.cstatus) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="文件名称" align="center" prop="fname" />
      <el-table-column label="创建人" align="center" prop="createBy" width="160"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button v-if="scope.row.cstatus === '5'"
            :disabled="scope.row.cstatus !== '5'"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleDownload(scope.row)"
          >下载</el-button>
          <el-button
            v-else
            :disabled="scope.row.cstatus !== '0'"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleSubmit(scope.row)"
          >提交</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 底部分页器 -->
    <pagination
      v-show="queryTotal>0"
      :total="queryTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <FileImportCpt ref="FileImportCpt" @uploadResult="fileUploadResult"></FileImportCpt>

  </div>
</template>

<script>
import { reqGetFubaoList, reqSubmit, reqDownoad } from "@/api/workflow/fubao";
import FileImportCpt from './FileImportCpt'

export default {
  components: {
    FileImportCpt,
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      queryTotal: 0,
      // 【请填写功能名称】表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchId: undefined,
        code: undefined,
      },
      autoFreshTimer: null
    };
  },
  computed: {
    statusText() {
      return val => {
        switch (val) {
          case '0': return '未提交'
          case '1': return '已提交'
          case '2': return '处理中'
          case '3': return '失败'
          case '4': return '导入中'
          case '5': return '导入失败'
        }
      }
    },
    statusType() {
      return val => {
        switch (val) {
          case '0': return 'warning'
          case '1': return 'success'
          case '2': return 'primary'
          case '3': return 'danger'
          case '4': return 'primary'
          case '5': return 'danger'
        }
      }
    }
  },
  created() {
    this.getList();
  },
  destroyed() {
    if (this.autoFreshTimer) {
      clearInterval(this.autoFreshTimer)
      this.autoFreshTimer = null
    }
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      reqGetFubaoList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.queryTotal = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.categoryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleImport() {
      this.$refs['FileImportCpt'].handleImport()
    },
    handleSubmit(row) {
      const that = this
      this.$confirm('是否确认提交名称为"' + row.fname + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        // that.$modal.loading("正在处理...");
        return reqSubmit({
          'batchId': row.batchId
        });
      }).then(() => {
        // that.$modal.closeLoading();
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(error => { console.warn(error) })
    },
    handleDownload(row) {
      const that = this
      this.$confirm('是否确认下载名称为"' + row.fname + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        that.download('fubao/export', {
          'batchId': row.batchId
        }, `IptError_${row.fname}`)
      })
    },
    fileUploadResult() {
      this.resetQuery()
      this.autoFreshStart()
    },
    autoFreshStart() {
      if (this.autoFreshTimer) {
        clearInterval(this.autoFreshTimer)
      }
      this.autoFreshTimer = setInterval(() => {
        reqGetFubaoList(this.queryParams).then(response => {
          this.dataList = response.rows;
          this.queryTotal = response.total;
        }).catch(error => {
          console.error(error)
          clearInterval(this.autoFreshTimer)
        })
      }, 5000);
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
